#include "qpolygonwidget.h"


#include "../../property/qabstractproperty.h"
#include "../../property/qpropertyfactory.h"
#include "../../shared/qcommonstruct.h"

#include <QPainter>
#include <QGradient>

QPolygonWidget::QPolygonWidget(QWidget *parent) :
    QWidget(parent)
{
}

void QPolygonWidget::setLineWidth(int lineWidth)
{
    if(lineWidth<1)
    {
        lineWidth=1;
    }
    if(m_line_width!=lineWidth)
    {
        m_line_width=lineWidth;
        update();
    }
}

int QPolygonWidget::lineWidth()
{
    return m_line_width;
}

void QPolygonWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    int line_style=property("line_style").toInt();
    QGradient g=property("line_color").value<QGradient>();
    QPen pen((Qt::PenStyle)line_style);
    pen.setBrush(QBrush(g));
    g=property("fill_color").value<QGradient>();
    pen.setWidth(m_line_width);
    pen.setJoinStyle(Qt::RoundJoin);
    p.setPen(pen);
    p.setBrush(QBrush(g));
    QRect re=this->rect();

    if(m_line_width>1)
    {
        p.setRenderHint(QPainter::Antialiasing,true);
    }

    QPointFs ps=property("points").value<QPointFs>();

    QPoint* pt=new QPoint[ps.size()];

    int wid=this->width()-1-m_line_width;
    int hei=this->height()-1-m_line_width;

    int _x=m_line_width/2;
    int _y=m_line_width/2;

    for(int i=0;i<ps.size();i++)
    {
        QPointF pf=ps.at(i);
        pt[i].setX(_x+(int)(wid*pf.x()));
        pt[i].setY(_y+(int)(hei*pf.y()));
    }
    p.drawPolygon(pt,ps.size());

    delete [] pt;
}


QPolygonWidgetHost::QPolygonWidgetHost(QAbstractHost *parent):
    QWidgetHost(parent)
{
    this->setProperty("need_frame",true);
    setProperty("accept_drop",false);
    setProperty("edit_property","points");
}

void QPolygonWidgetHost::create_object()
{
    m_object=new QPolygonWidget;
    m_object->setObjectName("polygonWidget");
}

QString QPolygonWidgetHost::get_show_name()
{
    return tr("Polygon");
}

QString QPolygonWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QPolygonWidgetHost::get_show_icon()
{
    return ":/images/polygon.png";
}

void QPolygonWidgetHost::init_property()
{
    QWidgetHost::init_property();

    remove_property("font");
    remove_property("enabled");
    remove_property("focusPolicy");
    remove_property("background");
    remove_property("border");
    remove_property("toolTip");
    remove_property("mouseMove");
    remove_property("mousePress");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Number");

    QPolygonWidget* wid=(QPolygonWidget*)m_object;
    if(pro!=NULL)
    {
        pro->set_property("name","line_width");
        pro->set_attribute("show_name",tr("Width"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_width",1);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("PointFs");
    if(pro!=NULL)
    {
        pro->set_property("name","points");
        pro->set_attribute("show_name",tr("Points"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("points",pro->get_value());
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","line_color");
        pro->set_attribute("show_name",tr("Color"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_color",pro->get_value());
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","fill_color");
        pro->set_attribute("show_name",tr("Fill Color"));
        pro->set_attribute("group","Attributes");
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        g.setColorAt(1,QColor(0,0,0,0));
        QVariant v;
        v.setValue<QGradient>(g);
        m_object->setProperty("fill_color",v);
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","line_style");
        pro->set_attribute("show_name",tr("Style"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="SolidLine";
        item.m_value=Qt::SolidLine;
        items.append(item);
        item.m_text="DashLine";
        item.m_value=Qt::DashLine;
        items.append(item);
        item.m_text="DotLine";
        item.m_value=Qt::DotLine;
        items.append(item);
        item.m_text="DashDotLine";
        item.m_value=Qt::DashDotLine;
        items.append(item);
        item.m_text="DashDotDotLine";
        item.m_value=Qt::DashDotDotLine;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->set_attribute("items",v);
        m_object->setProperty("line_style",Qt::SolidLine);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

void QPolygonWidgetHost::setLineWidth(int lineWidth)
{
    set_property_value("line_width",lineWidth);
}

int QPolygonWidgetHost::lineWidth()
{
    return get_property_value("line_width").toInt();
}

void QPolygonWidgetHost::setLineStyle(int lineStyle)
{
    set_property_value("line_style",lineStyle);
}

int QPolygonWidgetHost::lineStyle()
{
    return get_property_value("line_style").toInt();
}

qRegisterHost("basegraphics.polygon",QPolygonWidgetHost,BASE_GRAPHICS_INDEX+20)
